import { uuid } from "../../utils/common";

export default class {
  constructor(send2Ue, listen2Ue) {
    this.send2Ue = send2Ue;
    this.listen2Ue = listen2Ue;
    this.baseCmd = "";
    this.poiEvent = undefined;
    this.poiEventTypeMap = {
      1: "OnClicked",
      2: "OnMouseHovered",
      3: "OnMouseUnhovered",
    };
    this.poiEventMap = {
      OnClicked: [],
      OnMouseHovered: [],
      OnMouseUnhovered: [],
      all: [],
    };
  }
  addMouseClick(cb) {
    this.listen2Ue(`OnMouseClicked`, cb);
  }
  // 单例模式
  #temEvent = (res) => {
    if (this.poiEventMap[res.data.Event]) {
      this.poiEventMap[res.data.Event].forEach((item) => {
        item.func({
          ...res,
          eventId: item.id,
        });
      });
    }
    this.poiEventMap.all.forEach((item) => {
      item.func({
        ...res,
        eventId: item.id,
      });
    });
  };
  addPoiEvent(cb, type) {
    if (!this.PoiEvent) {
      this.listen2Ue(`/PoiManager/PoiEvent_Callback`, this.#temEvent);
      this.PoiEvent = true;
    }
    // 注册poi事件
    let EventId = "";
    if (type) {
      EventId = type + "_" + uuid(13);
      this.poiEventMap[this.poiEventTypeMap[type]].push({
        id: EventId,
        func: cb,
      });
    } else {
      EventId = "all_" + uuid(13);
      this.poiEventMap.all.push({
        id: EventId,
        func: cb,
      });
    }
    return EventId;
  }
  removePoiEventByid(id) {
    const type = id.split("_")[0];
    if (this.poiEventTypeMap[type]) {
      this.poiEventMap[this.poiEventTypeMap[type]] = this.poiEventMap[
        this.poiEventTypeMap[type]
      ].filter((item) => {
        return item.id !== id;
      });
    } else {
      this.poiEventMap.all = this.poiEventMap.all.filter((item) => {
        return item.id !== id;
      });
    }
  }
  removePoiEventAll() {
    this.poiEventMap.OnClicked = [];
    this.poiEventMap.OnMouseHovered = [];
    this.poiEventMap.OnMouseUnhovered = [];
    this.poiEventMap.all = [];
  }
}
